﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessComponents;
using BusinessComponents.Managers;

namespace WaterfrontLodge.WebForms.RoomTypeMgt
{
    public partial class RoomTypeAdd3 : RoomTypeAddController
    {
        IRoomTypeMgr typeMgr = ManagerFactory.RoomTypeMgr;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        override
        protected string getCurView()
        {
            return "RoomTypeAdd3";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                RoomType rt = getRoomTypeTransferObject();
                ManagerFactory.RoomTypeMgr.CreateRoom(rt);

                Response.Redirect(getNextView(), true);
            }
            catch(Exception ex){
                lblMsg.Text = "Failed to create a new Room Type.~r~n";
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (!uploadImg.HasFile)
            {
                lblStatus.Text = "Plase select Upload file first.";
            }
            else
            {
                RoomType rt = getRoomTypeTransferObject();

                lblStatus.Text = "";
                if (uploadImg.FileName != string.Empty)
                {
                    //check file type & size
                    string fileName = uploadImg.FileName;

                    if (typeMgr.IsValidExtension(fileName))
                    {
                        if (typeMgr.IsValidFileSize(uploadImg.FileBytes.Length))
                        {
                            rt.ImagePath = "~/Image/" + uploadImg.FileName;
                            uploadImg.SaveAs(Server.MapPath("~/Image/") + uploadImg.FileName);
                            imgPicture.ImageUrl = "~/Image/" + uploadImg.FileName;
                        }
                        else
                        {
                            lblStatus.Text = "Please upload Image file Less than 100 KB.";
                        }
                    }
                    else
                    {
                        lblStatus.Text = "Please upload jpg, jpeg, gif or png file only.";

                    }
                }
                else
                {
                    rt.ImagePath = string.Empty;
                }
            }
        }

    }
}